<?php

namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\Category;
use App\Models\Tag;
use App\Models\User;
use App\Models\Message;
use App\Models\Photo;
use App\Models\Comment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StatsController extends Controller
{
    /**
     * 仪表盘总览
     */
    public function dashboard(): JsonResponse
    {
        $stats = [
            'total_articles' => Article::count(),
            'published_articles' => Article::where('status', 'published')->count(),
            'total_categories' => Category::count(),
            'total_tags' => Tag::count(),
            'total_users' => User::count(),
            'total_messages' => Message::count(),
            'total_photos' => Photo::count(),
            'total_comments' => Comment::count(),
            'recent_articles' => Article::orderBy('created_at', 'desc')->take(5)->get(['id', 'title', 'created_at']),
            'recent_messages' => Message::orderBy('created_at', 'desc')->take(5)->get(['id', 'guest_name', 'content', 'created_at']),
            'recent_photos' => Photo::orderBy('created_at', 'desc')->take(5)->get(['id', 'title', 'url', 'created_at']),
        ];
        return response()->json([
            'data' => $stats,
            'message' => '获取统计数据成功',
        ], 200);
    }

    /**
     * 文章统计（按天/周/月/年分组）
     */
    public function articleStats(Request $request): JsonResponse
    {
        $period = $request->input('period', 'month');
        $query = Article::select(
            DB::raw('DATE(created_at) as date'),
            DB::raw('COUNT(*) as count')
        );
        switch ($period) {
            case 'week':
                $query->where('created_at', '>=', now()->subWeek());
                break;
            case 'month':
                $query->where('created_at', '>=', now()->subMonth());
                break;
            case 'year':
                $query->where('created_at', '>=', now()->subYear());
                break;
        }
        $stats = $query->groupBy('date')->orderBy('date')->get();
        return response()->json([
            'data' => $stats,
            'message' => '获取文章统计数据成功',
        ], 200);
    }

    /**
     * 分类统计（每个分类下文章数）
     */
    public function categoryStats(): JsonResponse
    {
        $stats = Category::withCount('articles')
            ->orderBy('articles_count', 'desc')
            ->get(['id', 'name', 'articles_count']);
        return response()->json([
            'data' => $stats,
            'message' => '获取分类统计数据成功',
        ], 200);
    }

    /**
     * 标签统计（每个标签下文章数）
     */
    public function tagStats(): JsonResponse
    {
        $stats = Tag::withCount('articles')
            ->orderBy('articles_count', 'desc')
            ->get(['id', 'name', 'articles_count']);
        return response()->json([
            'data' => $stats,
            'message' => '获取标签统计数据成功',
        ], 200);
    }

    /**
     * 留言统计（按天/周/月/年分组）
     */
    public function messageStats(Request $request): JsonResponse
    {
        $period = $request->input('period', 'month');
        $query = Message::select(
            DB::raw('DATE(created_at) as date'),
            DB::raw('COUNT(*) as count')
        );
        switch ($period) {
            case 'week':
                $query->where('created_at', '>=', now()->subWeek());
                break;
            case 'month':
                $query->where('created_at', '>=', now()->subMonth());
                break;
            case 'year':
                $query->where('created_at', '>=', now()->subYear());
                break;
        }
        $stats = $query->groupBy('date')->orderBy('date')->get();
        return response()->json([
            'data' => $stats,
            'message' => '获取留言统计数据成功',
        ], 200);
    }

    /**
     * 用户统计（按天/周/月/年分组）
     */
    public function userStats(Request $request): JsonResponse
    {
        $period = $request->input('period', 'month');
        $query = User::select(
            DB::raw('DATE(created_at) as date'),
            DB::raw('COUNT(*) as count')
        );
        switch ($period) {
            case 'week':
                $query->where('created_at', '>=', now()->subWeek());
                break;
            case 'month':
                $query->where('created_at', '>=', now()->subMonth());
                break;
            case 'year':
                $query->where('created_at', '>=', now()->subYear());
                break;
        }
        $stats = $query->groupBy('date')->orderBy('date')->get();
        return response()->json([
            'data' => $stats,
            'message' => '获取用户统计数据成功',
        ], 200);
    }

    /**
     * 图片统计（按天/周/月/年分组）
     */
    public function photoStats(Request $request): JsonResponse
    {
        $period = $request->input('period', 'month');
        $query = Photo::select(
            DB::raw('DATE(created_at) as date'),
            DB::raw('COUNT(*) as count')
        );
        switch ($period) {
            case 'week':
                $query->where('created_at', '>=', now()->subWeek());
                break;
            case 'month':
                $query->where('created_at', '>=', now()->subMonth());
                break;
            case 'year':
                $query->where('created_at', '>=', now()->subYear());
                break;
        }
        $stats = $query->groupBy('date')->orderBy('date')->get();
        return response()->json([
            'data' => $stats,
            'message' => '获取图片统计数据成功',
        ], 200);
    }

    /**
     * 评论统计（按天/周/月/年分组）
     */
    public function commentStats(Request $request): JsonResponse
    {
        $period = $request->input('period', 'month');
        $query = Comment::select(
            DB::raw('DATE(created_at) as date'),
            DB::raw('COUNT(*) as count')
        );
        switch ($period) {
            case 'week':
                $query->where('created_at', '>=', now()->subWeek());
                break;
            case 'month':
                $query->where('created_at', '>=', now()->subMonth());
                break;
            case 'year':
                $query->where('created_at', '>=', now()->subYear());
                break;
        }
        $stats = $query->groupBy('date')->orderBy('date')->get();
        return response()->json([
            'data' => $stats,
            'message' => '获取评论统计数据成功',
        ], 200);
    }
} 